package com.ciquan.server.controller;

import com.ciquan.server.bean.WorksInfo;
import com.ciquan.server.bean.WorksRecommend;
import com.ciquan.server.service.WorksInfoService;
import com.ciquan.server.service.WorksRecommendService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by kakalee on 15/3/27.
 */
@Controller
@RequestMapping("/worksRecommend")
public class WorksRecommendController extends AbstractController {

    @Resource
    private WorksRecommendService worksRecommendService;

    @Resource
    private WorksInfoService worksInfoService;

    @RequestMapping("/list")
    public ModelAndView list(ModelMap modelMap) {
        List<WorksRecommend> worksRecommends = worksRecommendService.selectList();
        modelMap.put("worksRecommends", worksRecommends);
        return new ModelAndView("worksRecommendList", modelMap);
    }

    @RequestMapping("add")
    public ModelAndView add(ModelMap modelMap) {
        List<WorksInfo> worksInfos = worksInfoService.selectList();
        modelMap.put("worksInfos", worksInfos);
        return new ModelAndView("addWorksRecommend", modelMap);
    }

    @RequestMapping("saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request) {
        int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
        WorksRecommend worksRecommend = new WorksRecommend();
        worksRecommend.setWorksId(worksId);
        worksRecommendService.insert(worksRecommend);
        return new ModelAndView("redirect:/worksRecommend/list");
    }

    @RequestMapping("edit")
    public ModelAndView edit(HttpServletRequest request, ModelMap modelMap) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
        WorksRecommend worksRecommend = new WorksRecommend();
        worksRecommend.setId(id);
        worksRecommend.setWorksId(worksId);
        List<WorksInfo> worksInfos = worksInfoService.selectList();

        modelMap.put("worksRecommend", worksRecommend);
        modelMap.put("worksInfos", worksInfos);
        return new ModelAndView("editWorksRecommend", modelMap);
    }

    @RequestMapping("saveEdit")
    public ModelAndView saveEdit(HttpServletRequest request) {
        int id = ServletRequestUtils.getIntParameter(request, "id", 0);
        int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
        WorksRecommend worksRecommend = new WorksRecommend();
        worksRecommend.setId(id);
        worksRecommend.setWorksId(worksId);
        worksRecommendService.update(worksRecommend);
        return new ModelAndView("redirect:/worksRecommend/list");
    }
}
